<?php

class IndexController extends App_Controller_Action
{
	private $publickey = '6LcogwoAAAAAAOJm8FmbMZWXBVc0O_5LgAO4bs4h';
	private $privatekey = '6LcogwoAAAAAADf7guDDvM_ImAcjq8zqeMc3f52O';
		
    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        
    }
    
    public function loginAction()
    {
    	$this->_redirector = $this->_helper->getHelper('Redirector');
    	$request = $this->getRequest();

		if ($this->getUserId()) {
			$this->_redirector->gotoSimple("index", "member");
		}
    	
		$form = new Form_Login();
        $this->view->form = $form;        

        if ($this->getRequest()->isPost()) {

            $formData = $this->getRequest()->getPost();

            if ($form->isValid($formData)) {

                $username = $form->getValue('username');
                $password = $form->getValue('password');                
                $session = $this->getSession();

                // create components for authentication
                $storage = new App_Auth_Storage_Session($session);
                $adapter = new App_Auth_Adapter_Web($username, $password);
                $auth = Zend_Auth::getInstance();
                $auth->setStorage($storage);

                // authenticate the user (note: authorization is in Access Plugin)
                $result = $auth->authenticate($adapter);
                if (!$result->isValid()) {

                    // invalid credentials
                    $form->populate(array('username' => $username));
                    $this->view->formError = 'Invalid username and/or password.';
                    return; // re-render the login form
                }
                else {
					
                	$user = Doctrine_Core::getTable("Users")->findOneByUsername($username);
                	
                	if (!$user->active) {
	                    // valid credentials but has not been activated by System Administrator
	                    $form->populate(array('username' => $username));
	                    $this->view->formError = 'Your account is not yet activated.';
	                    return; // re-render the login form
                	}
                	$user->updateLastLogin();
                }
                $this->_redirector->gotoSimple("index", "member");

            } 
            else{
                $form->populate($formData);
            }
        }
    }
    
	public function logoutAction()
    {
    	$session = $this->getSession();
        $session->unsetAll();

        $this->_redirect('/index');
    }
    
	public function signupAction()
    {	
    	$this->_redirector = $this->_helper->getHelper('Redirector');
    	$request = $this->getRequest();

		if ($this->getUserId()) {
			$this->_redirector->gotoSimple("index", "member");
		}
    	
    	$form = new Form_Signup();
        $this->view->form = $form;
        
    	if ($this->getRequest()->isPost()) {
    		
            $formData = $this->getRequest()->getPost();
            
    		if ($form->isValid($formData)) {
	    		//$recaptcha = new Zend_Service_ReCaptcha($this->publickey, $this->privatekey);			 
				//$result = $recaptcha->verify($this->_getParam('recaptcha_challenge_field'),	$this->_getParam('recaptcha_response_field'));				
				//if (!$result->isValid()) {
					$user = new Users();
					$user->fromArray($form->getValues(true));
					$user->id_role = 'MB';
					$user->gender = 'N';
					$user->age = 0;
					$user->id_ethnicity = 10;
					$user->active = 1;					
					$user->allow_vote = 0;
					$user->voted = 0;
					$user->save();
			    	$user->refresh();
	
	                // send the user to the login page
	                $this->_redirect("index/login");
						
				//} else {
				//	$this->view->formError = 'Please try to match the 2 words shown above, or try the audio version.';
				//}
	        } else{
	        	$formData['password'] = '';
				$formData['confirmPassword'] = '';
				$form->populate($formData);
	        }
        }
    }
    
    public function uploadAction()
    {
    	// disabled the layout
    	$this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);
    	
    	// Uploading Document File on Server 
		$upload = new Zend_File_Transfer_Adapter_Http ( );
		$upload->setDestination ( APPLICATION_PATH . '/../public/images' );
		try {
			// upload received file(s)
			$upload->receive ();
		} catch ( Zend_File_Transfer_Exception $e ) {
			$e->getMessage ();
		}

		// lets deal with the upload image
		$file = $upload->getFileName ( 'flashUploadFiles' );
		$name = basename($file);
		
	   	$data = '';
	   			
		$data .='file='.$file.',name='.$name;
		
        echo $data;
        
        exit;
    }
}

